#include "YarnBall.h"

YarnBall::YarnBall(LPDIRECT3DDEVICE9 d3ddev, b2World * worldPhysics)
{
	this->d3ddev = d3ddev;
	this->world_physics = worldPhysics;
}

YarnBall::~YarnBall()
{
	//if( pMesh != NULL )
	//	pMesh->Release();

	/* A COPY FROM CMESH DESTRUCTOR And the reason why the code here is commented out
		I seem to have an inheritance problem when is comes to deleting the objects memory. It seems that it already gets deleted
		in the YarnBall class where I make this exact same call. The computer must be calling both destructors. I comment out
		the one in YarnBall. (I don't really know if that's what's going on but it seems that way)
	*/
}

void YarnBall::build()
{
	this->Load ("../../Mesh/yarnballScene.x", d3ddev); //Loads yarnball mesh!?
	this->createPhysics (CIRCLE, DYNAMIC); //sets physics
	this->setMass (100); //weight
	this->setFriction(100); // more traction; not so slippery when increasing the value
	this->setScale(0.5, 0.5); //shrink the yarnball by a 4th of it's original size
	this->setElastic (0.25f); //set elasticity; bounce height

	this->setPosition(0, 2); // set the yarballs default/starting position to (0,2)
	
}

void YarnBall::Render(LPDIRECT3DDEVICE9 d3ddev)
{
	D3DXMATRIXA16 matWorld;
	D3DXMATRIXA16 matTrans;
	D3DXMATRIXA16 matRot;
	D3DXMATRIXA16 matScale;

	if( phys_body != NULL )
	{
		//_cprintf("IsSleeping:%i\n", this->phys_body->IsSleeping());

		D3DXMatrixTranslation( &matTrans, phys_body->GetPosition().x, phys_body->GetPosition().y, 70.0f );
		D3DXMatrixScaling( &matScale, sx, sy, sz );
		D3DXMatrixRotationYawPitchRoll( &matRot, 
			                            D3DXToRadian(yaw), 
			                            D3DXToRadian(pitch), 
										phys_body->GetAngle() );
	}
	else
	{
		D3DXMatrixTranslation( &matTrans, x, y, z-layer );
		D3DXMatrixScaling( &matScale, sx, sy, sz );
		D3DXMatrixRotationYawPitchRoll( &matRot, 
			                            D3DXToRadian(yaw), 
			                            D3DXToRadian(pitch), 
			                            D3DXToRadian(roll) );
	}

	matWorld = matRot * matScale * matTrans * cameraMatrix;

	//////////////////////////////
	D3DXMATRIXA16 inWorldMat = matWorld;
	D3DXMATRIXA16 inProjMat;
	D3DXMATRIXA16 inViewMat;
	d3ddev->GetTransform( D3DTS_PROJECTION, &inProjMat);
	d3ddev->GetTransform( D3DTS_VIEW, &inViewMat);
	
	D3DXVECTOR3 Temp;
	ZeroMemory(&Temp, sizeof( D3DXVECTOR3 ) );
	D3DVIEWPORT9 viewPort;
	d3ddev->GetViewport(&viewPort);
	D3DXVec3Project(&ScreenPos, &Temp, &viewPort, &inProjMat, &inViewMat, &inWorldMat);
	//_cprintf("YarnBall ScreenSpace Position:%f,%f\n", ScreenPos.x, ScreenPos.y);
	///////////////////////////////


	d3ddev->SetTransform( D3DTS_WORLD, &matWorld );

	if( texture != NULL )
	d3ddev->SetTexture(0, this->texture->GetTexture());

	if( pMesh != NULL )
	pMesh->DrawSubset(0);
}

void YarnBall::movement(float x, float y)
{
	this->addRotForce (x);
}

void YarnBall::jump(float x)
{
		this->addForce (0, x);
}